package com.ojoin.trade.supplyloan_core.web.controller;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.excel.ExcelTmpl;
import com.ojoin.trade.common.web.controller.BaseController;
import com.ojoin.trade.common.web.controller.BaseControllerImpl;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotalVo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotalVoCompare;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotalVoCompareForExport;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanStatTotalService;

@Controller
@RequestMapping("/operate/tbslloanstattotal")
public class SlLoanStatTotalController extends
		BaseControllerImpl<TbSlLoanStatTotal> implements
		BaseController<TbSlLoanStatTotal> {

	private org.slf4j.Logger log = LoggerFactory
			.getLogger(SlLoanStatTotalController.class);

	@Autowired
	private TbSlLoanStatTotalService service;

	@Override
	protected BaseService<TbSlLoanStatTotal> getBaseService() {

		return service;
	}

	/*
	 * 电商业务数据统计
	 */
	@ResponseBody
	@RequestMapping(value = "/querynotlogin", method = RequestMethod.GET)
	public Result querySupplyChainPlatformUserCanNotLogin(int joinid) {

		try {
			List<TbSlLoanStatTotalVo> li = service
					.queryTbSlLoanStatTotalByJoinidAndTime(10,
							TimeUtils.getCurDate());
			return new Result(Result.Status.OK, li);
		} catch (Exception e) {
			log.error("", e);
			return new Result(Result.Status.ERROR, "查询电商业务数据统计失败");
		}
	}

	/*
	 * 导出数据
	 */
	@ResponseBody
	@RequestMapping(value = "/export", method = {RequestMethod.GET,RequestMethod.POST})
	public void export(HttpServletResponse response, String platformName,
			String merchantUserId, String statDate, String endDate,
			Pageable page) { 

		try {
			//endDate = "2015-12-25";
			//statDate = "2015-12-24";

			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(TimeUtils.getCurDate()+"数据统计.xls", Constants.ENCODING));  
			
			List<TbSlLoanStatTotalVoCompare> li = service
					.queryCompareByPlatformAndMerchantUserIDAndDate(platformName,
							merchantUserId, statDate, endDate, null);
			
			List<TbSlLoanStatTotalVoCompareForExport> lire =  new ArrayList<TbSlLoanStatTotalVoCompareForExport>();
			
			if(!CommonUtils.isEmpty(li)){
				Iterator<TbSlLoanStatTotalVoCompare> it = li.iterator();
				while(it.hasNext()){
					TbSlLoanStatTotalVoCompare co = it.next();
					TbSlLoanStatTotalVoCompareForExport ne = new TbSlLoanStatTotalVoCompareForExport();
					ne.setCompare(co);
					lire.add(ne);
				}
			}
			ExcelTmpl et=new ExcelTmpl("supply",response.getOutputStream());
			 
			et.wrapData(lire);
			 
			et.closeWbook();
			
			
		} catch (Exception e) {
			LogUtils.info(log, "导出出错");
			LogUtils.error(log, e);
		}
	}

	/*
	 * 根据公司名称，平台，时间查询统计数据
	 */
	@ResponseBody
	@RequestMapping(value = "/queryByPlatformAndMerchantUserIDAndDate", method = {
			RequestMethod.POST, RequestMethod.GET })
	public Result queryByPlatformAndMerchantUserIDAndDate(
			HttpServletRequest request, String platformName,
			String merchantUserId, String startDate, String endDate,
			Pageable page) {
		List<TbSlLoanStatTotalVoCompare> li = null;
		try{
			log.info("startDate"+startDate+"------endDate"+endDate+"-----settlementState");
			li = service
					.queryCompareByPlatformAndMerchantUserIDAndDate(platformName,
							merchantUserId, startDate, endDate, page);

			
		}catch(Exception e){
			LogUtils.info(log, "查询统计数据出错");
			LogUtils.error(log, e);
			return new Result(Result.Status.OK, "查询统计数据出错"); 
		}
		
		return new Result(Result.Status.OK, li);

	}

	/*
	 * 根据公司名称，平台，时间查询统计数据
	 */
	@ResponseBody
	@RequestMapping(value = "/queryByMerchantUserIDAndDate", method = {
			RequestMethod.POST, RequestMethod.GET })
	public Result queryByPlatformAndMerchantUserIDAndDate(
			HttpServletRequest request, String merchantUserId, String statDate) {
		log.info("*****merchantUserId：" + merchantUserId + "****statDate:"
				+ statDate + "**" + request.getParameter("statDate"));

		try {
			List<TbSlLoanStatTotalVo> li = service
					.queryByPlatformAndMerchantUserIDAndDate(null,
							merchantUserId, statDate);
			return new Result(Result.Status.OK, li);
		} catch (Exception e) {
			log.error("", e);
			return new Result(Result.Status.ERROR, "查询电商业务数据统计失败");
		}

	}

}
